package com.mgkj.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mgkj.entity.Purth;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * @author : ssy
 * @date: 2024-02-28
 * @Description:
 */
@Mapper
public interface PURTHMapper extends BaseMapper<Purth> {
    @DS("demo")
    @Select("SELECT MD002 FROM INVMD WHERE MD001 = #{cd004} ")
    String getJJDW(String cd004);

    @DS("demo")
    @Select("SELECT COUNT(*)\n" +
            "FROM QMSTA\n" +
            "LEFT JOIN PURCD ON CD001 = TA001 AND CD002 = TA002 AND CD003 = TA003\n" +
            "LEFT JOIN PURCC ON CD001 = CC001 AND CC002 = CD002\n" +
            "LEFT JOIN PURMA ON MA001 = CC005 \n" +
            "WHERE TAC01 > 0 and (CD018-CD020) >0 \n" +
            "AND TA001=#{dhjydb} AND TA002 = #{dhjydh} AND TA003 = #{xh} AND CC005 = #{tg005}")
    Integer isExistToTheInvoice(String dhjydb, String dhjydh, String xh, String tg005);

    @DS("demo")
    @Select("SELECT TA001,TA002,TA003,CD004,CD005,CD006," +
            "TAC01,TA008,TA007,TA025,TAC02,TA026,CD013,CD007, " +
            "CD016,CD010,CD011,CD012,MA064,CD023,TAC03,TA041, " +
            "CD031,CD032,TA042,CD021,TA031,TAC04,TA028 " +
            "FROM QMSTA\n" +
            "LEFT JOIN PURCD ON CD001 = TA001 AND CD002 = TA002 AND CD003 = TA003\n" +
            "LEFT JOIN PURCC ON CD001 = CC001 AND CC002 = CD002\n" +
            "LEFT JOIN PURMA ON MA001 = CC005 \n" +
            "WHERE TAC01 > 0 and (CD018-CD020) >0 \n" +
            "AND TA001=#{dhjydb} AND TA002 = #{dhjydh} AND TA003 = #{xh} AND CC005 = #{tg005}")
    Map<String, Object> Select(String dhjydb, String dhjydh, String xh, String tg005);

    @DS("demo")
    @Select("SELECT " +
            "    CASE WHEN MB148 = COALESCE(MD002, MB148) THEN 1 ELSE 0 END AS is_same \n" +
            "FROM INVMB \n" +
            "LEFT JOIN INVMD ON MD001 = MB001 \n" +
            "WHERE " +
            "    MB001 = #{ph}")
    Boolean isSameUnit(String ph);

    @DS("demo")
    @Select("SELECT \n" +
            "    CAST(#{sum} * (MD003/MD004) AS DECIMAL(18, 2)) as sl\n" +
            "FROM \n" +
            "    INVMB \n" +
            "LEFT JOIN \n" +
            "    INVMD ON MD001 = MB001 \n" +
            "WHERE \n" +
            "    MB001 = #{cd004} ")
    Double calculate(@Param("sum") Double sum, @Param("cd004") String cd004);

    @DS("demo")
    Boolean insertPurth(Purth purth);
}

